import { PageContainer } from '@ant-design/pro-components';
import UserForm from "./components/userForm"
import {useEffect,useState} from "react"
import {useLocation,history} from "umi"
import {getUserById,editUserApi} from  "@/services/user"
import {message} from "antd"
export default function EditUser(props){
    // 编辑用户页面 不显示在侧边栏
    const location = useLocation();
    console.log(location)
    const targetId = location.state.id;
    const [userInfo,setUserInfo] = useState({})
    useEffect(()=>{
        // 请求
        console.log(targetId)

        if(targetId){
            console.log(targetId)
            getUserById(targetId).then(res=>{
                console.log(res.data)
                setUserInfo(res.data)
            })
        }
    },[targetId])
    function submit(tar){
        console.log("传递的数据",tar)
        // 调用接口修改
        editUserApi(tar._id,tar).then(res=>{
            console.log("看看有没有回复",res)
        })
        message.success("编辑用户成功");
        history.push('/user/userList')
    }
    // 拿到目标id  
    return (
        <PageContainer header={{
            breadcrumb: {
              items: [
                {
                  title: '用户',
                },
                {
                  title: '编辑用户',
                },
              ],
            },
          }} >
              <UserForm type="edit" initData={userInfo} submit={submit}></UserForm>
          </PageContainer>
    )
}